/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package itproject.clients.gui;

import java.util.Enumeration;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author tim
 */
public class EBMenuTree extends JTree {

    private static EBMenuTree EasyBookingMenuTree = null;
    private static EBInterface ebi;

    private EBMenuTree(TreeNode root) {
        // Passes the nodes to the super class
        super(root);
    }

    // Singelton implementation
    public static EBMenuTree EasyBookingMenuTree(EBInterface ebif) {
        ebi = ebif;
        if (EasyBookingMenuTree == null) {
           EBMenuTree tree = new EBMenuTree(EasyBookingMenuTree.createNodes());
           tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
           tree.addTreeSelectionListener(ebi);
           
            return tree;
        }
        return EasyBookingMenuTree;
    }

    // creates nodes of the tree
    private static TreeNode createNodes(){

         DefaultMutableTreeNode root = new DefaultMutableTreeNode("EasyBooking");

            {
                DefaultMutableTreeNode CustomerNodes = new DefaultMutableTreeNode("Customer");
                root.add(CustomerNodes);

                CustomerNodes.add(new DefaultMutableTreeNode("Create Customer"));
                CustomerNodes.add(new DefaultMutableTreeNode("Change Customer"));
                CustomerNodes.add(new DefaultMutableTreeNode("Cancel Customer"));

                DefaultMutableTreeNode flightNodes = new DefaultMutableTreeNode("Fights");
                root.add(flightNodes);
                flightNodes.add(new DefaultMutableTreeNode("Create Flight"));
                flightNodes.add(new DefaultMutableTreeNode("Change Flight"));
                flightNodes.add(new DefaultMutableTreeNode("Cancel Flight"));

                root.add(flightNodes);
            }
         return root;

    }
  
            
            

    // http://www.java2s.com/Code/Java/Swing-JFC/ExpandingorCollapsingAllNodesinaJTreeComponent.htm
    public void expandAll() {
    TreeNode root = (TreeNode) this.getModel().getRoot();
    expandAll(this, new TreePath(root));
  }

  private void expandAll(JTree tree, TreePath parent) {
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
      for (Enumeration e = node.children(); e.hasMoreElements();) {
        TreeNode n = (TreeNode) e.nextElement();
        TreePath path = parent.pathByAddingChild(n);
        expandAll(tree, path);
      }
    }
    tree.expandPath(parent);
    // tree.collapsePath(parent);
  }
}
